﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HotelProject
{
    public partial class Results : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            results.InnerHtml = "";
            MySQLDatabaseConnector dataBase = new MySQLDatabaseConnector();

            HotelSearchQuery searchQuery = Session["searchQuery"] as HotelSearchQuery;

            if (!IsPostBack)
            {
                DropDownItemGenerator.populateHotelsInDropDown(hotelSelection);
                DropDownItemGenerator.populateViewsDropDown(viewDropDown);
                DropDownItemGenerator.populateBedSizesInDropDownBox(bedSizeDropDown);
                DropDownItemGenerator.populatePricesInDropDownBox(maxPriceDropDownList);
                DropDownItemGenerator.populatePricesInDropDownBox(minPriceDropDownList);
                DropDownItemGenerator.populateFloorNumbersInDropDownBox(floorNumberDropDownList);
                DropDownItemGenerator.populateRoomNumbersInDropDownBox(roomNumberDropDownList);
            }

            if(searchQuery != null)
            {
                hotelRoom[] availableRooms = HotelSearch.search(searchQuery);
                
                results.InnerHtml += "<h2>"+ availableRooms.Length + " Rooms Available</h2> <ul>";
                if (availableRooms.Length > 1)
                {
                    

                    foreach (hotelRoom room in availableRooms)
                    {
                        results.InnerHtml += "<li id=\"resultItem\">" + 
                                                room.ToStringForHTML() + 
                                                "<a id=\"bookLink\" href=\"Book.aspx?RoomID=" + 
                                                room.roomID + 
                                                "\"><img src=\"Images\\book.png\"/></a></li>";
                    }
                }
                else
                {
                    showNoResultsMessage();
                }
            }
            else 
            { 
                showNoResultsMessage();  
            }
        }

        private void showNoResultsMessage()
        {
            results.InnerHtml = "Sorry No Resuts Found";
        }

        protected void Submit_Click(object sender, EventArgs e)
        {
            DateTime checkInDate, checkOutDate;
            DateTime.TryParse(txtDate1.Text, out checkInDate);
            DateTime.TryParse(txtDate2.Text, out checkOutDate);

            int minPrice, maxPrice, roomNumber, floorNumber;

            int.TryParse(minPriceDropDownList.SelectedValue, out minPrice);
            int.TryParse(maxPriceDropDownList.SelectedValue, out maxPrice);
            int.TryParse(roomNumberDropDownList.SelectedValue, out roomNumber);
            int.TryParse(floorNumberDropDownList.SelectedValue, out floorNumber);

            HotelSearchQuery newSearchQuery = new HotelSearchQuery()
            {
                hotelID = int.Parse(hotelSelection.SelectedValue),
                checkInDate = checkInDate,
                checkOutDate = checkOutDate,
                minPrice = minPrice,
                maxPrice = maxPrice,
                bedSize = bedSizeDropDown.SelectedValue,
                view = viewDropDown.SelectedValue,
                roomNumber = roomNumber,
                floorNumber = floorNumber

            };


            Session["searchQuery"] = newSearchQuery;
            
        }

        
    }
}